<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>部门添加</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="./css/x-admin.css" media="all">
</head>
<body>
	<div class="x-nav">
		<span class="layui-breadcrumb"> <a><cite>首页</cite></a> <a><cite>部门管理</cite></a>
			<a><cite>部门信息</cite></a> <a><cite>部门信息添加</cite></a>
		</span> <a class="layui-btn layui-btn-small"
			style="line-height: 1.6em; margin-top: 3px; float: right"
			href="javascript:location.replace(location.href);" title="刷新"><i
			class="layui-icon" style="line-height: 30px">ဂ</i></a>
	</div>
	<div class="x-body">
		<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
			<div class="layui-tab-content">
				<div class="layui-tab-item layui-show">
					<form class="layui-form layui-form-pane" >
						<div class="layui-form-item">
                            <label class="layui-form-label">
                                <span class='x-red'>*</span>部门名
                            </label>
                            <div class="layui-input-block">
                                <input type="text" name="deptname" id="deptname" autocomplete="off" 
                                class="layui-input" >
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">
                                <span class='x-red'>*</span>主管号
                            </label>
                            <div class="layui-input-block">
                                <select name="empno" id="empno"></select>
                            </div>
                        </div>
						<div class="layui-form-item">
							<button class="layui-btn" lay-submit="" lay-filter="*" >添加</button>
						</div>
						<div style="height: 300px;"></div>
					</form>
				</div>
			</div>
		</div>
	</div>
	<script src="./lib/layui/layui.js" charset="utf-8"></script>
	<script src="./js/x-layui.js" charset="utf-8"></script>
	<script src="js/jquery-2.0.3.js"></script>
	<script>
	 layui.use(['layer','form','element'], function(){
	   $ = layui.jquery;//jquery
       var layer = layui.layer;//弹出层
       var form = layui.form();
       var element = layui.element();
       
       //监听提交
       form.on('submit(*)', function(data){
         var deptname = $("input[name='deptname']").val();
         var empno = $("select[name='empno']").val();
         if(deptname == ''){
             layer.msg("部门名不能为空！",{icon:5,time:2000});
             return false;
         }
         if(empno == ''){
        	 layer.msg("主管号不能为空！",{icon:5,time:2000});
             return false; 
         }
         $.ajax({
             type:"post",
             url:"DeptServlet",
             data:{
            	 deptname : deptname,
            	 empno : empno,
            	 op:"add"
             },
             success:function (data) {
                 if(data.code == 200){
                     layer.msg(data.msg,{icon:6,time:1000});
                     setTimeout(function(){
                         window.parent.location.reload();
                         var index = parent.layer.getFrameIndex(window.name);
                         parent.layer.close(index);
                     },1000);
                     return false;
                 }else{
                     layer.msg(data.msg,{icon:5,time:2000});
                     return false;
                 }
             },
             dataType:"json"
         });
         return false;
       });
       $(function(){
			$("#deptname").blur(function(){
				ajaxCheckName();
			});
			
			$("#empno").blur(function(){
				ajaxCheckEmp();
			});
		});
       
       function ajaxCheckName(){
			$.ajax({
				url:"DeptServlet",
				type:"get",
				data:{
					deptname : $("#deptname").val(),
					op : "checkDeptname"
				},
				success:function(data){
					if(data == "部门名可用"){
						layer.msg(data,{icon:1,time:1000});
					}else{
						layer.msg(data,{icon:5,time:2000});
					}
				},
				dataType:"json"
			});
		}
       
       function ajaxCheckEmp(){
			$.ajax({
				url:"DeptServlet",
				type:"get",
				data:{
					empno : $("#empno").val(),
					op : "checkEmpno"
				},
				success:function(data){
					if(data == "主管编号可用"){
						layer.msg(data,{icon:1,time:1000});
					}else{
						layer.msg(data,{icon:5,time:2000});
					}
				},
				dataType:"json"
			});
		}   
       
     })
	</script>
	<script type="text/javascript">
	 	var noManager = "暂无主管";
		$(function(){
			$.get("EmpServlet?op=ajaxReq",function(data){
				$("#empno").append("<option value="+noManager+">"+ noManager + "</option>");
				$.each(data,function(i,o){
					$("#empno").append("<option value="+o.empNo+">"+ o.empNo + "</option>");
				})
			})
		})
	</script>
</body>
</html>